/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FICSInterface.java
 *
 * Created on Apr 28, 2010, 3:59:54 PM
 */

package FICS;

import TelnetClient.TelnetInterface;
import bbchess.MainGui;
import bbchess.util.ImageLoader;
import bbchess.util.SoundLoader;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.KeyStroke;

/**
 *
 * @author Tyler
 */
public class SimpleFICSInterface extends javax.swing.JFrame {

    private TelnetInterface telnetInterface;
    private InputMap inputMap;
    private ActionMap actionMap;
    private MainGui gui;
    private ArrayList<String> playerNames;
    private PlayerTableModel playerTableModel;

    /** Creates new form FICSInterface */
    public SimpleFICSInterface(MainGui gui) {
        playerTableModel = new PlayerTableModel();

        initComponents();
        this.gui = gui;
        setLocationRelativeTo(null);
        
        // Start code for adding the enter button to the inputmap, in order to
        // prevent a newline from being created in inputField every time its pressed
        inputMap = inputField.getInputMap();
        actionMap = inputField.getActionMap();

        // Start code for adding the enter button to the inputmap, in order to
        // prevent a newline from being created in inputField every time its pressed
        inputMap = inputField.getInputMap();
        actionMap = inputField.getActionMap();

        Object transferTextActionKey = "TRANSFER_TEXT";
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), transferTextActionKey);

        actionMap.put(transferTextActionKey, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                sendChatToTelnet();
            }
        });
        // End
    }

    @Override
    public void dispose() {
        telnetInterface.stopTelnet();
        telnetInterface.dispose();
        super.dispose();
    }

    @Override
    public void hide() {
        gui.setNetworkViewMenuItemSelected(false);
        super.hide();
    }

    public void setFriendsList(Object[] names) {
        //friendsList.setListData(names);
    }

    public void setPlayerList(ArrayList<String> inPlayerNames) {
        playerNames = (ArrayList<String>) inPlayerNames.clone();

        //playerTable.validate();
        //playerTable.repaint();
        //playerScrollPane.revalidate();
    }

    public void setTelnetInterface(TelnetInterface telnetInterface) {
        this.telnetInterface = telnetInterface;
    }

    public void setConsoleMenuItemSelected(boolean choice) {
        consoleMenuItem.setSelected(choice);
    }

    public void sendToGameChat(String text) {
        gameChatArea.append(text);
        SoundLoader.chatMessage();
    }

    public void setNameLabel(String inText) {
        nameLabel.setText(inText);
        repaint();
    }

    public void updateGameList(String inText) {
        //gamesTextArea.append(inText);
    }

    public void updateSoughtList(String inText) {
        //soughtTextArea.append(inText);
    }

    public void clearGameList() {
        //gamesTextArea.setText("");
    }

    public void clearSoughtList() {
        //soughtTextArea.setText("");
    }

    public void sendChatToTelnet() {
        // if game chat tab is selected
        //if (chatTabbedPane.getSelectedIndex() == 0) {
            String chat = "say " + inputField.getText();
            gameChatArea.append("me: " + inputField.getText() + "\n");
            telnetInterface.tellTelnet(chat);
        //}

        inputField.setText("");
    }

    class MyTable extends javax.swing.JTable {
        public MyTable() {
            super();

            setFillsViewportHeight(true);
        }
    }

    class PlayerTableModel extends javax.swing.table.AbstractTableModel {

        public int getRowCount() {
            try {
                return playerNames.size();
            } catch (Exception ex) {
                return 0;
            }
        }

        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                
                Object playerName = "";

                if (playerNames != null) {
                    try {
                        playerName = playerNames.get(rowIndex);
                    } catch (Exception ex) {
                    }
                }

                return playerName;
            }

            else {
                return "ELO";
            }
        }

        @Override
        public String getColumnName(int c) {
            if (c == 1) {
                return "Rating";
            }

            else {
                return "Name";
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        nameLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        gameChatArea = new javax.swing.JTextArea();
        inputField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        logoutMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        seekMenuItem = new javax.swing.JCheckBoxMenuItem();
        jMenu3 = new javax.swing.JMenu();
        consoleMenuItem = new javax.swing.JCheckBoxMenuItem();

        setTitle("Internet Chess (FICS)");
        setIconImage(ImageLoader.getIconImage());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        nameLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        nameLabel.setText("...");
        jToolBar1.add(nameLabel);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setMaximumSize(new java.awt.Dimension(32767, 22));
        jPanel1.setMinimumSize(new java.awt.Dimension(100, 22));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusLabel)
                .addContainerGap(520, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(statusLabel)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton2.setText("Find a game");
        jButton2.setToolTipText("Seek a new match.");
        jButton2.setMaximumSize(new java.awt.Dimension(25, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(23, 23));
        jButton2.setPreferredSize(new java.awt.Dimension(25, 25));
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton2MouseExited(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton9.setText("Challenge a player");
        jButton9.setToolTipText("Challenge a player to a new match.");
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton9MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton9MouseExited(evt);
            }
        });
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton9))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton9))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton2, jButton9});

        gameChatArea.setColumns(20);
        gameChatArea.setEditable(false);
        gameChatArea.setLineWrap(true);
        gameChatArea.setRows(5);
        gameChatArea.setWrapStyleWord(true);
        jScrollPane1.setViewportView(gameChatArea);

        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Reconnect");
        jMenu1.add(jMenuItem1);

        logoutMenuItem.setText("Logout");
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(logoutMenuItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Actions");

        seekMenuItem.setText("Seek");
        seekMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seekMenuItemActionPerformed(evt);
            }
        });
        jMenu2.add(seekMenuItem);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("View");

        consoleMenuItem.setText("Console");
        consoleMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consoleMenuItemActionPerformed(evt);
            }
        });
        jMenu3.add(consoleMenuItem);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(284, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputField, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sendButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void seekMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seekMenuItemActionPerformed
        if (seekMenuItem.isSelected()) {
            telnetInterface.tellTelnet("seek");
        }
        else {
            telnetInterface.tellTelnet("unseek");
        }
    }//GEN-LAST:event_seekMenuItemActionPerformed

    private void consoleMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consoleMenuItemActionPerformed
        if (consoleMenuItem.isSelected()) {
            telnetInterface.setVisible(true);
        }
        else {
            telnetInterface.setVisible(false);
        }
    }//GEN-LAST:event_consoleMenuItemActionPerformed

    private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed
        gui.setEnableNetworkViewMenuItem(false);
        telnetInterface.tellTelnet("quit");
        gui.clearTelnetInterface();
        dispose();
    }//GEN-LAST:event_logoutMenuItemActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        new MatchDialog(gui, true, telnetInterface).setVisible(true);
}//GEN-LAST:event_jButton9ActionPerformed

    private void jButton9MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton9MouseExited
        statusLabel.setText("");
}//GEN-LAST:event_jButton9MouseExited

    private void jButton9MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton9MouseEntered
        statusLabel.setText("Challenge a player to a match.");
}//GEN-LAST:event_jButton9MouseEntered

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new SeekDialog(gui, true, telnetInterface).setVisible(true);
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseExited
        statusLabel.setText("");
}//GEN-LAST:event_jButton2MouseExited

    private void jButton2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseEntered
        statusLabel.setText("Start looking for a new match.");
}//GEN-LAST:event_jButton2MouseEntered

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        sendChatToTelnet();
}//GEN-LAST:event_sendButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBoxMenuItem consoleMenuItem;
    private javax.swing.JTextArea gameChatArea;
    private javax.swing.JTextField inputField;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JCheckBoxMenuItem seekMenuItem;
    private javax.swing.JButton sendButton;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables

}
